package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"github.com/kataras/iris"
	"github.com/kataras/iris/mvc"
	"io/ioutil"
	"log"
	"wow-web/config"
	"wow-web/controller"
	"wow-web/dao"
)

func main() {
	file := flag.String("conf", "", "config file path")
	flag.Parse()
	if *file == "" {
		flag.PrintDefaults()
		return
	}
	bytes, e := ioutil.ReadFile(*file)
	if e != nil {
		log.Fatalln(e)
	}
	conf := config.GetConfig()
	e = json.Unmarshal(bytes, conf)
	if e != nil {
		log.Fatalln(e)
	}

	dao.ConnString = fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", conf.Mysql.User, conf.Mysql.Password, conf.Mysql.Host, conf.Mysql.Port, conf.Mysql.Database)

	app := newApp()
	app.Run(iris.Addr(fmt.Sprintf(":%d", conf.ServerPort)))
}
func newApp() *iris.Application {
	// $ go get -u github.com/shuLhan/go-bindata/...
	// $ go-bindata ./static/...
	// $ go build
	app := iris.New()
	//app.StaticEmbedded("/web", "./static", Asset, AssetNames)
	tmpl := iris.HTML("./static", ".html")
	tmpl.Binary(Asset, AssetNames)
	app.RegisterView(tmpl)
	mvc.New(app.Party("/wow")).Handle(new(controller.WowController))
	mvc.New(app.Party("/api")).Handle(new(controller.ApiController))
	app.Get("/", redirect)
	return app
}

func redirect(ctx iris.Context) {
	ctx.Redirect("/wow")
}
